$(document).ready(function() {
	moment.locale("zh-cn");
	window.currentpage = 1;
	ready();
	
});

function ready() {
	queryUserInfo(1);//查询第一页
	//绑定事件
	bind();
	//表单校验
	check();
	showUserName();
}

function initDormLocation() {
	$.ajax({
		url:"/dormms/super/getNoAdminGoodStaff",
        type:"post",
        success:function(data){
        	console.log(data);
        	for(var i=0; i<data.length; i++){
        		 $("#insertStaffId").append("<option value="+ data[i].staffId+">"+data[i].staffName+"</option>");
        	}
        }
	});
	$.ajax({
		url:"/dormms/super/getAllGoodInfo",
        type:"post",
        success:function(data){
        	console.log(data);
        	for(var i=0; i<data.length; i++){
        		$("#insertGoodAdmin").append("<option value="+ data[i].goodInfoId+">"+data[i].goodName+ "," +data[i].goodLocation +"</option>");
        	}
        }
	});
}

function bind() {
	$("#pageSize").bind("change",function(){
		queryUserInfo(window.currentpage);
	});
	$("#searchGoodInfoBygoodName").bind("click",function(){
		window.currentpage = 1;
		queryUserInfo(window.currentpage);
	});
	$('#addGoodInfoModal').on('hide.bs.modal', function (){
		$("#insertGoodInfoId").val("");
		$("#insertGoodInfoName").val("");
		$("#insertGoodInfoId-error").remove();
		$("#insertGoodInfoName-error").remove();
		$("#insertGoodInfoLocation").val("");
		$("#insertGoodInfoDescr").val("");
		$("#insertGoodInfoLocation-error").remove();
		$("#insertGoodInfoDescr-error").remove();
	});
	$('#addGoodAdminModal').on('show.bs.modal', function (){
		initDormLocation();
	});
	$('#addGoodAdminModal').on('hide.bs.modal', function (){
		$("#insertGoodAdmin").val("");
		$("#insertStaffId").val("");
		$("#insertGoodAdmin-error").remove();
		$("#insertStaffId-error").remove();
		$("#insertGoodAdmin option").remove();
		$("#insertStaffId option").remove();
	});
	$('#updateGoodInfoModal').on('hide.bs.modal', function (){
		$("#updateGoodInfoId").val("");
		$("#updateGoodInfoName").val("");
		$("#updateGoodInfoId-error").remove();
		$("#updateGoodInfoName-error").remove();
		$("#updateGoodInfoLocation").val("");
		$("#updateGoodInfoDescr").val("");
		$("#updateGoodInfoLocation-error").remove();
		$("#updateGoodInfoDescr-error").remove();
	});
	$('#updatePasswordModel').on('hide.bs.modal', function (){
		$("#oldPassword").val("");
		$("#password").val("");
		$("#password_again").val("");
		$("#oldPassword-error").remove();
		$("#password-error").remove();
		$("#password_again-error").remove();
	});
	$('input[name="selectAllDormAdmin"]').bind("click",function(){
		if($(this).is(':checked')){
			$('input[name="selectDormAdmin"]').each(function(){   
                $(this).prop("checked",true);  
            });
		}else{
			$('input[name="selectDormAdmin"]').each(function(){   
                $(this).removeAttr("checked",false);
            });
		}
	});
}

function check(){
	addCheck();
	addAdminCheck();
	updateCheck();
	updatePasswordCheck();
}


function addCheck(){
	//添加校验
	$("#addGoodInfo").validate({
		rules:{
			insertGoodInfoId:{
				required:true,
				digits:true,
				minlength:6,
				maxlength:6
			},
			insertGoodInfoName:{
				required:true,
			},
			insertGoodInfoLocation:{
				required:true,
			}
		},
		messages:{
			insertGoodInfoId:{
				required:"请输编号",
				digits:"请输入正确格式的编号",
				minlength:"请输入正确格式的编号",
				maxlength:"请输入正确格式的编号"
			},
			insertGoodInfoName:{
				required:"请输入财产名称",
			},
			insertGoodInfoLocation:{
				required:"请输入安装位置",
			}
		},
		submitHandler:function(form){
			var insertGoodInfoId = $("#insertGoodInfoId").val();
			var insertGoodInfoName = $("#insertGoodInfoName").val();
			var insertGoodInfoLocation = $("#insertGoodInfoLocation").val();
			var insertGoodInfoDescr = $("#insertGoodInfoDescr").val();
			$.ajax({
				type:'post',
				url:'insertGoodInfo',
				data:{"goodInfoId":insertGoodInfoId,"goodName":insertGoodInfoName,"goodLocation":insertGoodInfoLocation,"goodDescr":insertGoodInfoDescr},
				success:function(data){
					queryUserInfo(window.currentpage);
				},
				error:function(){
					console.log("1");
				}
			});
			$('#addGoodInfoModal').modal('hide');
		},
		invalidHandler: function(form, validator) {return false;}
	})
}

function addAdminCheck(){
	$("#addGoodAdmin").validate({
		rules:{
			insertGoodAdmin:{
				required:true,
			},
			insertStaffId:{
				required:true,
			},
		},
		messages:{
			insertGoodAdmin:{
				required:"请选择财类别",
			},
			insertStaffId:{
				required:"请选择维修人员",
			},
		},
		submitHandler:function(form){
			var insertStaffId = $("#insertStaffId").val();
			var insertGoodAdmin = $("#insertGoodAdmin").val();
			$.ajax({
				type:'post',
				url:'insertGoodAdminInfo',
				data:{"goodId":insertGoodAdmin,"staffId":insertStaffId},
				success:function(data){
					queryUserInfo(window.currentpage);
				},
				error:function(){
					console.log("1");
				}
			});
			$('#addGoodAdminModal').modal('hide');
		},
		invalidHandler: function(form, validator) {return false;}
	})
}

function updateCheck(){
	//修改校验
	$("#updateGoodInfo").validate({
		rules:{
			updateGoodInfoId:{
				required:true,
				digits:true,
				minlength:6,
				maxlength:6
			},
			updateGoodInfoName:{
				required:true,
			},
			updateGoodInfoLocation:{
				required:true,
			},
		},
		messages:{
			updateGoodInfoId:{
				required:"请输入财产编号",
				digits:"请输入正确格式的编号",
				minlength:"请输入正确格式的编号",
				maxlength:"请输入正确格式的编号"
			},
			updateGoodInfoName:{
				required:"请输入财产名称",
			},
			updateGoodInfoLocation:{
				required:"请输入安装位置",
			}
		},
		submitHandler:function(form){
			var updateGoodInfoId = $("#updateGoodInfoId").val();
			var updateGoodInfoName = $("#updateGoodInfoName").val();
			var updateGoodInfoLocation = $("#updateGoodInfoLocation").val();
			var updateGoodInfoDescr = $("#updateGoodInfoDescr").val();
			$.ajax({
				type:'post',
				url:'updateGoodInfoBySuper',
				data:{"goodInfoId":updateGoodInfoId,"goodName":updateGoodInfoName,"goodLocation":updateGoodInfoLocation,"goodDescr":updateGoodInfoDescr},
				success:function(data){
					queryUserInfo(window.currentpage);
				},
				error:function(){
					console.log("1");
				}
			});
			$('#updateGoodInfoModal').modal('hide');
		},
		invalidHandler: function(form, validator) {return false;}
	})
}
function updatePasswordCheck(){
	$("#updatePassword").validate({
		rules:{
			oldPassword:{
				required:true
			},
			password:{
				required:true
			},
			password_again:{
				required:true,
				equalTo: "#password"
			}
		},
		messages:{
			oldPassword:{
				required:"请输入旧密码"
			},
			password:{
				required:"请输入新密码"
			},
			password_again:{
				required:"请输入新密码",
				equalTo: "两次输入不相同"
			}
		},
		submitHandler:function(form){
			var oldPassword = $("#oldPassword").val();
			var password = $("#password").val();
			$.ajax({
				type:'post',
				url:'updatePassword',
				data:{"oldPassword":oldPassword,"password":password},
				success:function(data){
				},
				error:function(){
					console.log("1");
				}
			});
			$('#updatePasswordModel').modal('hide');
		},
		invalidHandler: function(form, validator) {return false;}
	})
}

function queryUserInfo(page){
	var currPage = page;
	var pageSize = $("#pageSize").val();
	var goodName = $("#goodName").val();
	$.ajax({
		url:"/dormms/super/queryGoodInfoByPageBySuper",
        type:"post",
        data:{"currPage":currPage,"pageSize":pageSize,"goodName":goodName},
        success:function(data){
        	console.log(data);
        	$("#goodInfoList").empty();
        	$("#total").text("共有"+data.total+"条记录");
        	for(var i=0; i<data.list.length; i++){
        		 var trTemp = $("<tr></tr>");
        		 //trTemp.append("<td>"+ "<input type='checkbox' name='selectStu' value='" + data.list[i].stuId + "'></td>");
        		 trTemp.append("<td>"+ data.list[i].goodInfoId +"</td>");
        		 trTemp.append("<td>"+ data.list[i].goodName +"</td>");
        		 trTemp.append("<td>"+ data.list[i].goodLocation +"</td>");
        		 trTemp.append("<td>" + "<button type='button' class='btn btn-outline-success btn-sm' onclick='getDormAdminInfoGet(this)'>查看</button>" 
        				 +"<button type='button' class='btn btn-outline-primary btn-sm' onclick='getDormAdminInfoUpdate(this)'>修改</button>" 
        				 + "</td>");
        		 trTemp.appendTo("#goodInfoList");
        	}
        	queryUserRoleByPage(data);
        }
	});
}

function queryUserRoleByPage(data) {
	var options = {
			bootstrapMajorVersion: 3,
			currentPage: data.pageNum,
            totalPages: data.pages,
            numberofPages: 5,
            itemTexts: function(type, page, current) { //修改显示文字
                switch (type) {
                case "first":
                    return "&laquo;";
                case "prev":
                    return "&lt;";
                case "next":
                    return "&gt;";
                case "last":
                    return "&raquo;";
                case "page":
                    return page;
                }
            },
            onPageClicked: function (event, originalEvent, type, page) { //异步换页
            	queryUserInfo(page);
            	window.currentpage=page;
            },
	};
	$('#page').bootstrapPaginator(options); 
}


//获取当前行数据(修改)
function getDormAdminInfoUpdate(obj) {
	var x = $(obj).parent().parent().find("td");
	var dormAdmininfo = x.eq(0).text();
	$.ajax({
		url:"/dormms/super/getGoodAllStaff",
		type:"get",
		data:{'goodInfoId':dormAdmininfo},
		success:function(data){
			console.log(data);
			$("#updateGoodInfoId").val(data.goodInfo.goodInfoId);
			$("#updateGoodInfoName").val(data.goodInfo.goodName);
			$("#updateGoodInfoLocation").val(data.goodInfo.goodLocation);
			$("#updateGoodInfoDescr").val(data.goodInfo.goodDescr);
			$("#updateStaffInfoList").empty();
        	for(var i=0; i<data.staffInfos.length; i++){
        		 var trTemp = $("<tr></tr>");
        		 //trTemp.append("<td>"+ "<input type='checkbox' name='selectStu' value='" + data.list[i].stuId + "'></td>");
        		 trTemp.append("<td id='findDormAdminId'>"+ data.staffInfos[i].staffId +"</td>");
        		 trTemp.append("<td>"+ data.staffInfos[i].staffName +"</td>");
        		 if(data.staffInfos[i].staffTel!=null){
        			 trTemp.append("<td>"+ data.staffInfos[i].staffTel +"</td>");
        		 }else{
        			 trTemp.append("<td></td>");
        		 }
        		 trTemp.append("<td><button type='button' class='btn btn-outline-danger btn-sm' onclick='deleteGoodAdmin(this)'>删除</button></td>");
        		 trTemp.appendTo("#updateStaffInfoList");
        	}
			$('#updateGoodInfoModal').modal('show');
		}
	});
}
//获取当前行数据(查看)
function getDormAdminInfoGet(obj) {
	var x = $(obj).parent().parent().find("td");
	var dormAdmininfo = x.eq(0).text();
	$.ajax({
		url:"/dormms/super/getGoodAllStaff",
		type:"get",
		data:{'goodInfoId':dormAdmininfo},
		success:function(data){
			console.log(data);
			$("#getGoodInfoId").val(data.goodInfo.goodInfoId);
			$("#getGoodInfoName").val(data.goodInfo.goodName);
			$("#getGoodInfoLocation").val(data.goodInfo.goodLocation);
			$("#getGoodInfoDescr").val(data.goodInfo.goodDescr);
			$("#getGoodAdminInfoList").empty();
        	for(var i=0; i<data.staffInfos.length; i++){
        		 var trTemp = $("<tr></tr>");
        		 trTemp.append("<td>"+ data.staffInfos[i].staffId +"</td>");
        		 trTemp.append("<td>"+ data.staffInfos[i].staffName +"</td>");
        		 if(data.staffInfos[i].staffTel!=null){
        			 trTemp.append("<td>"+ data.staffInfos[i].staffTel +"</td>");
        		 }else{
        			 trTemp.append("<td></td>");
        		 }
        		 trTemp.appendTo("#getGoodAdminInfoList");
        	}
			$('#getGoodAdminInfoModal').modal('show');
		}
	});
}

function deleteGoodAdmin(obj){
	var x = $(obj).parent().parent().find("td");
	var staffId = x.eq(0).text();
	$.ajax({
		url:"deleteGoodAdmin",
		type:"get",
		data:{'staffId':staffId},
		success:function(data){
			var goodInfoId = $("#updateGoodInfoId").val();
			$.ajax({
				url:"getGoodAllStaff",
				type:"get",
				data:{'goodInfoId':goodInfoId},
				success:function(data){
					console.log(data);
					$("#updateGoodInfoId").val(data.goodInfo.goodInfoId);
					$("#updateGoodInfoName").val(data.goodInfo.goodName);
					$("#updateGoodInfoLocation").val(data.goodInfo.goodLocation);
					$("#updateGoodInfoDescr").val(data.goodInfo.goodDescr);
					$("#updateStaffInfoList").empty();
		        	for(var i=0; i<data.staffInfos.length; i++){
		        		 var trTemp = $("<tr></tr>");
		        		 //trTemp.append("<td>"+ "<input type='checkbox' name='selectStu' value='" + data.list[i].stuId + "'></td>");
		        		 trTemp.append("<td id='findDormAdminId'>"+ data.staffInfos[i].staffId +"</td>");
		        		 trTemp.append("<td>"+ data.staffInfos[i].staffName +"</td>");
		        		 if(data.staffInfos[i].staffTel!=null){
		        			 trTemp.append("<td>"+ data.staffInfos[i].staffTel +"</td>");
		        		 }else{
		        			 trTemp.append("<td></td>");
		        		 }
		        		 trTemp.append("<td><button type='button' class='btn btn-outline-danger btn-sm' onclick='deleteGoodAdmin(this)'>删除</button></td>");
		        		 trTemp.appendTo("#updateStaffInfoList");
		        	}
				}
			});
		}
	});
}

function showPasswordModel() {
	$('#updatePasswordModel').modal('show');
}

function showUserName() {
	$.ajax({
		url:"getSuperInfo",
		type:"get",
		success:function(data){
			$("#userId").text(data.userId);
		}
	});
}